function defineHeatingTarifWindow(){
	hf.widget.HeatingTarifWindow = Ext.extend(hf.widget.TarifWindow, {
		constructor: function(config) {
			
			hf.widget.HeatingTarifWindow.superclass.constructor.apply(this, arguments);
		    
		    this.on('change', function(field, newValue, oldValue){
		    	this.fieldDataChanged(field, newValue, oldValue);
		    });
			
			this.relayEvents(this.areaField, ['change']);
			this.relayEvents(this.coefficientField, ['change']);
			this.relayEvents(this.summaField, ['change']);
			
		},
		
		fieldDataChanged: function(field, newValue, oldValue){
			if(field.ref == this.summaField.ref){
				if(newValue != oldValue){
					Ext.Msg.alert(labels['attention.alert.title'], messages['message.tariff.summa.change'], 
						function(btn, text){
        					if (btn != Ext.MessageBox.buttonText.ok.toLowerCase()){
        						field.setValue(oldValue);
        					}
        			});	
				}
			}else{
				this.calculateSumma();
			}
		},
		
		calculateSumma: function(){
			var area = this.areaField.getValue();
			var coefficient = this.coefficientField.getValue();
			this.summaField.setValue(area*coefficient);
		},
		
		prepareSaving: function(){
			this.calculateSumma();
			return true;
		}
	});
	Ext.reg('heating-tarif-window', hf.widget.HeatingTarifWindow);
};


function createHeatingTab(){
	defineHeatingTarifWindow();

	//store for tarif records
	var tarifStore = {
		xtype: 'app-store',
		viewAction: 'heatingTarif/view.action',
		addAction: 'heatingTarif/update.action',
		updateAction: 'heatingTarif/update.action',
		deleteAction: 'heatingTarif/delete.action',
		columnArray: [
         	{name: indexes['id'], type: INTEGER}, 
         	{name: indexes['enabled'], type: INTEGER},
         	{name: indexes['modelId'],  type: INTEGER},
         	{name: 'tarifName', type: STRING},
         	{name: 'area', type: INTEGER},
         	{name: 'coefficient',  type: FLOAT},
         	{name: 'summa',  type: FLOAT}
      	],
      	defaultData: {id : FAKE_ID, enabled : ENTITY_STATE_ENABLED, modelId : FAKE_ID, tarifName: '', area: 0, coefficient: 0.00, summa: 0.00}
	};
	
	//store for communal records
	var recordStore = {
		xtype: 'app-store',
		viewAction: 'heating/view.action',
		addAction: 'heating/update.action',
		updateAction: 'heating/update.action',
		deleteAction: 'heating/delete.action',
		columnArray: [
         	{name: indexes['id'], type: INTEGER}, 
         	{name: indexes['enabled'], type: INTEGER},
         	{name: indexes['summa'], type: FLOAT},
         	{name: indexes['date'],  type: DATE},
         	{name: indexes['modelId'],  type: INTEGER},
         	{name: indexes['month'],  type: INTEGER},
         	{name: indexes['year'],  type: INTEGER},
         	{name: 'tarifId',  type: INTEGER},
         	{name: 'monthYear',  type: STRING}
      	],
      	defaultData: {id : FAKE_ID, enabled : ENTITY_STATE_ENABLED, modelId : FAKE_ID, tarifId: 0, date: new Date(), 
		    year: new Date().getFullYear(), month: new Date().getMonth(), summa: 0.00, monthYear: ''}
	};
	
	var tarifWindow = {
		xtype: 'heating-tarif-window',
		height:200,
		formItems: [
			{xtype: 'id-hidden-field'},
			{xtype: 'enabled-hidden-field'},
			{xtype: 'modelid-hidden-field'},
			{xtype: 'textfield', fieldLabel: labels['label.tarif.name'], name: 'tarifName', allowBlank: false, blankText: labels['label.value.empty']},
		    {
		     xtype: 'integer-number-field', 
		     ref: '../areaField', 
		     fieldLabel: 'Площадь',
		     name: 'area',
		     minValue: 1, 
		     minText: labels['label.value.positive'],
		     maxValue: 10000,
		     maxText: labels['label.value.over'] + ' 10000'
		    },
		    {
		     xtype: 'double-number-field',
		     ref: '../coefficientField',
		     fieldLabel: labels['label.heating.tariff.onem2'],
		     name: 'coefficient'
		    },
		    {
		     xtype: 'double-number-field', 
		     ref: '../summaField', 
		     fieldLabel: labels['label.summa'],
		     name: 'summa',
		     minValue: 1, 
		     minText: labels['label.value.positive']
		    }
	    ]
	    
	};
	
	var communalWindow = {
		xtype: 'communal-window',
		height: 230,
		formItems: [
			{xtype: 'id-hidden-field'},
			{xtype: 'enabled-hidden-field'},
			{xtype: 'modelid-hidden-field'},
			{xtype: 'tarif-combo', store: tarifStore},
		    {xtype: 'communal-date-field'},
		    {xtype: 'year-combo'},
		    {xtype: 'month-combo'},
		    {xtype: 'double-number-field', ref: '../summaField', fieldLabel: labels['label.summa'], name: 'summa', readOnly: true}			    
       ]
	};
	
	//grid for displaying tarif data
	var tarifGrid = {
		xtype: 'app-grid',
		ref: '../../tarifGrid',
		store: tarifStore,
   		colModel: new Ext.grid.ColumnModel({
       		defaults: {width: 70, sortable: true, hideable: true},
        	columns: [
        		new Ext.grid.RowNumberer({header: '№'}),
        		{xtype: 'gridcolumn', id: 'tarifName', dataIndex: 'tarifName', header: labels['label.tarif.name'], width: 100, tooltip: qtips['qtips.column.tarif.name'], hideable: false},
       			{xtype: 'numbercolumn', id: 'area', dataIndex: 'area', header: labels['label.heating.tariff.area'], format: '0', tooltip: qtips['qtips.heating.tariff.area']},
       			{xtype: 'numbercolumn', id: 'coefficient', dataIndex: 'coefficient', header: labels['label.heating.tariff.onem2'], tooltip: qtips['qtips.heating.tariff.onem2']},
       			{xtype: 'numbercolumn', id: 'summa', dataIndex: 'summa', header: labels['label.summa'], tooltip: labels['label.summa']},
       			{xtype: 'action-column'}
       		]
 		}),
 		windowType: tarifWindow,
 		autoExpandColumn: 'tarifName',
 		anchor: '100% 40%'
	};
	
	//grid for displaying communal records
	var recordGrid = {
		xtype: 'communal-grid',
		title: labels['label.heating.grid.title'],
		ref: '../recordGrid',
		store: recordStore,
   		colModel: new Ext.grid.ColumnModel({
       		defaults: {width: 70, sortable: true, hideable: true},
        	columns: [
        		new Ext.grid.RowNumberer({header: '№'}),
			    {xtype: 'datecolumn', id: indexes['date'], dataIndex: indexes['date'], header: labels['label.date'], tooltip: qtips['qtips.column.electricity.date']},
       			{xtype: 'gridcolumn', id: indexes['year'], dataIndex: indexes['year'], header: labels['label.year'], tooltip: qtips['qtips.column.year']},
       			{xtype: 'gridcolumn', id: indexes['month'], dataIndex: indexes['month'], header: labels['label.month'], tooltip: qtips['qtips.column.month'],
       			renderer: function(val, x, rec){return months[val][1];}},
       			{xtype: 'numbercolumn', id: indexes['summa'], dataIndex: indexes['summa'], header: labels['label.summa'], tooltip: labels['label.summa']},
       			{xtype: 'action-column'}
       		]
 		}),
 		windowType: communalWindow,
 		autoExpandColumn: indexes['summa'],
 		flex: 1
	};
	
	var tab = {
		xtype: 'app-tab', 
		title: labels['label.heating.tab.title'],
		//iconCls: 'icon-heating',
		items: [{
			layout: {type: 'hbox', align: 'stretch'},
    		anchor: '100%, 100%',
    		items: [recordGrid, {flex: 1, layout: 'anchor', items: [tarifGrid, {xtype: 'chart-panel'}]}]
		}]
	};
	return tab;
};